import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_motorcade_app/pages/vehicle_manage/item/vehicle_item.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';
import 'package:nyh_motorcade_app/widgets/search_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'search_vehicle_controller.dart';

class SearchVehiclePage extends StatelessWidget {
  final controller = Get.put(SearchVehicleController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SearchVehicleController>(
      builder: (controller) {
        return Scaffold(
          backgroundColor: Colours.color_background,
          resizeToAvoidBottomInset: false,
          appBar: SearchAppBar(
            controller: controller.searchController,
            focusNode: controller.searchFocusNode,
            context: context,
            hintText: "请输入车牌号",
            onChanged: (value) {
              controller.searchVehicle(value);
            },
          ),
          body: Container(
            child: Stack(
              fit: StackFit.expand,
              alignment: Alignment.center,
              children: [buildHistoryRecordWidget(), buildSearchResultWidget()],
            ),
          ),
        );
      },
    );
  }

  Widget buildHistoryRecordWidget() {
    return Visibility(
      visible: controller.keyword.isEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: Column(
          children: [
            Container(
              height: 40.w,
              padding: EdgeInsets.only(left: 16.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(
                    "历史搜索",
                    style: TextStyle(
                      color: Colours.color_text_theme,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  Visibility(
                    visible: controller.historyRecordList.isNotEmpty,
                    child: IconButton(
                      onPressed: () {
                        controller.deleteHistoryRecord();
                      },
                      icon: Image.asset("images/ic_delete.png"),
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child: ListView.separated(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                itemBuilder: (context, index) {
                  return buildHistoryRecordItemWidget(index);
                },
                separatorBuilder: (context, index) {
                  return Divider(color: Colours.color_background, height: 1.w, thickness: 1.w);
                },
                itemCount: controller.historyRecordList.length,
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildHistoryRecordItemWidget(int index) {
    String string = controller.historyRecordList[index];
    return Material(
      child: InkWell(
        onTap: () {
          controller.searchController.text = string;
          controller.searchVehicle(string);
        },
        child: Padding(
          padding: EdgeInsets.symmetric(vertical: 17.w, horizontal: 16.w),
          child: Text(
            string,
            style: TextStyle(color: Colours.color_text_theme, fontSize: 14.sp),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }

  Widget buildSearchResultWidget() {
    return Visibility(
      visible: controller.keyword.isNotEmpty,
      child: Positioned(
        left: 0,
        top: 0,
        right: 0,
        bottom: 0,
        child: MultiStateWidget(
          child: Column(
            children: [
              Container(
                height: 40.w,
                padding: EdgeInsets.only(left: 16.w),
                child: Row(
                  children: [
                    Text(
                      "相关车辆(${controller.orderCount})",
                      style: TextStyle(
                        color: Colours.color_text_theme,
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: SmartRefresher(
                  controller: controller.refreshController,
                  enablePullDown: false,
                  enablePullUp: true,
                  onLoading: controller.onLoading,
                  child: ListView.builder(
                    shrinkWrap: true,
                    physics: BouncingScrollPhysics(),
                    itemCount: controller.vehicleInfoList.length,
                    itemBuilder: (context, index) {
                      return VehicleItem(controller.vehicleInfoList[index], keyword: controller.keyword, focusNode: controller.searchFocusNode);
                    },
                  ),
                ),
              ),
            ],
          ),
          state: controller.multiState,
        ),
      ),
    );
  }
}
